package com.example.mvptest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;


import com.example.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class JokeActivity extends AppCompatActivity  implements Contract.iView{

    int  page=0;
    JokePresenter jokePresenter;
    private TextView tvJk;
    private TextView tvPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.joke_layout);
        tvJk = (TextView) findViewById(R.id.tvJk);
        tvPage = (TextView) findViewById(R.id.tvPage);
        jokePresenter = new JokePresenter(this);

        jokePresenter.getJoke(String.valueOf(page));
        tvPage.setText(String.valueOf(page));


//        Observable.timer(5, TimeUnit.SECONDS)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Consumer<Long>() {
//                    @Override
//                    public void accept(Long aLong) throws Exception {
////                    Intent intent = new Intent(this, SubscriptVipActivity.class);
//////                    startActivity(intent);
//////                    finish();
//                        Toast.makeText(JokeActivity.this, "delay", Toast.LENGTH_SHORT).show();
//                    }
//                });
    }

    @Override
    public void loading() {

        ToastUtil.showToast("loading···");
    }

    @Override
    public void dissmissLoading() {
        ToastUtil.dismissToast();
    }

    @Override
    public void success(JokeBean bean) {
        StringBuffer titles = new StringBuffer();
        List<JokeBean.DataBean.DatasBean> beans=bean.getData().getDatas();
        if (beans!=null) {
            for (int i = 0; i < beans.size(); i++) {
                titles=titles.append((i+1)+"，"+beans.get(i).getTitle()).append("\n");
            }
        }

        tvJk.setText(titles);

    }

    @Override
    public void failed(String ss) {
        //可能点击数次超过最大页数，置1
        page=0;
        tvPage.setText(String.valueOf(page));
        Toast.makeText(this,ss,Toast.LENGTH_SHORT).show();
    }

    public void next(View view) {
//        startActivity(new Intent(this,MainA));
//        EventBus.getDefault().post(new MessageWrap("hello"));
//        finish();\

        //页数+1;
        page++;
        tvPage.setText(String.valueOf(page));
        jokePresenter.getJoke(String.valueOf(page));
    }
    public void previous(View view) {
//        startActivity(new Intent(this,MainA));
//        EventBus.getDefault().post(new MessageWrap("hello"));
//        finish();
        if (page>0) {
            page--;
            jokePresenter.getJoke(String.valueOf(page));
            tvPage.setText(String.valueOf(page));
        }else {
            Toast.makeText(this,"当前是起始页",Toast.LENGTH_SHORT).show();
        }

    }
}